package com.guqj.nio.netty.serialization.jdk;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import com.guqj.nio.netty.serialization.jdk.pojo.SubscribeReq;
import com.guqj.nio.netty.serialization.jdk.pojo.SubscribeRes;

public class SubscribeServerHandler extends SimpleChannelInboundHandler<Object> {

	@Override
	protected void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception {
		
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		SubscribeReq req = (SubscribeReq) msg;
		System.out.println("Service accept client subscribe req: [" + req + "]");
		ctx.writeAndFlush(doResp(req.getSubReqID()));
	}

	private SubscribeRes doResp(int subReqId){
		SubscribeRes res = new SubscribeRes();
		res.setSubReqID(subReqId);
		res.setRespCode(0);
		res.setDesc("Netty book order success");
		return res;
	}
	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		ctx.close();
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		ctx.flush();
	}

	
}
